package com.zwim.common.util;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Marker;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 *该类是为替换slf4j框架中的日志ID
 */
@Slf4j
public class LogIdMarker implements Marker {
    public static final String IM_MARKER_NAME="im_marker";
    public static  String TRACE_LOG_ID="TRACE_LOG_ID";
    public static String OTHER_MSG="OTHER_MSG";
    Map<MarkerKey,String> logIDMarkers;

    /**
     *
     */
    public static enum MarkerKey{
        ABC("ABC"),
        MARKER("MARKER");
        String key;
        private MarkerKey(String key){
            this.key=key;
        }
        public void setKey(String key){
            this.key=key;
        }
        public String getKey(){
            return this.key;
        }
    }
    LogIdMarker(MarkerKey markerKey,String value){
        this(new MarkerKey[]{markerKey},new String[]{value});
    }
    LogIdMarker(MarkerKey[] markerKey,String[] values){
        this.logIDMarkers=new HashMap(markerKey.length);
        this.setMarkerKeys(markerKey,values);
    }

    public void setMarkerKeys(MarkerKey[] markerKeys, String[] values) {
        String[] newValues = values;
        if (values.length < markerKeys.length) {
            newValues = new String[markerKeys.length];
            System.arraycopy(values, 0, newValues, 0, values.length);
            Arrays.fill(newValues, values.length, markerKeys.length, "");
        }

        for(int i = 0; i < markerKeys.length; ++i) {
            this.logIDMarkers.put(markerKeys[i], newValues[i]);
        }

    }

    public Map<MarkerKey, String> getBestpayMarkers() {
        return this.logIDMarkers;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer(80);
        Iterator ite = this.logIDMarkers.keySet().iterator();

        while(ite.hasNext()) {
            MarkerKey key = (MarkerKey)ite.next();
            sb.append("\"").append(key).append("\":\"").append((String)
                    this.logIDMarkers.get(key)).append("\"");
            if (ite.hasNext()) {
                sb.append(",");
            }
        }

        return sb.toString();
    }

    @Override
    public String getName() {
        return "im_marker";
    }
    @Override
    public void add(Marker marker) {

    }
    @Override
    public boolean remove(Marker marker) {
        return false;
    }
    @Override
    public boolean hasChildren() {
        return false;
    }
    @Override
    public boolean hasReferences() {
        return false;
    }
    @Override
    public Iterator<Marker> iterator() {
        return null;
    }
    @Override
    public boolean contains(Marker marker) {
        return false;
    }
    @Override
    public boolean contains(String s) {
        return false;
    }

}
